package com.jl.jlapp.mvp.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.alipay.sdk.app.PayTask;
import com.jl.jlapp.R;
import com.jl.jlapp.eneity.AuthTask;
import com.jl.jlapp.eneity.CancleOrderModel;
import com.jl.jlapp.eneity.OrderDetail;
import com.jl.jlapp.eneity.PayResult;
import com.jl.jlapp.eneity.PostModel;
import com.jl.jlapp.eneity.WXOrderInfo;
import com.jl.jlapp.eneity.WXOrderInfo.ResultDataBean;
import com.jl.jlapp.nets.Api;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.nets.Transformer;
import com.jl.jlapp.popu.ClearHistorySearhPopu;
import com.jl.jlapp.utils.SuperTextView;
import com.jl.jlapp.utils.Tools;
import com.jl.jlapp.wxapi.WXPayEntryActivity;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class PayOrderActivity extends Activity implements OnClickListener {
    private static final int SDK_PAY_FLAG = 1;
    private IWXAPI api;
    @BindView(R.id.back)
    ImageView back;
    private long chaoshitime;
    ClearHistorySearhPopu clearHistorySearhPopu;
    private long countdownTime;
    int fromDetailPage = -1;
    Handler handler = new Handler();
    private boolean isChoosePayMode = false;
    boolean isNotOvertime = true;
    @SuppressLint({"HandlerLeak"})
    private Handler mHandler = new Handler() {
        public void handleMessage(Message paramAnonymousMessage) {
            PayResult payResult = new PayResult((Map) paramAnonymousMessage.obj);
            payResult.getResult();
            if (TextUtils.equals(payResult.getResultStatus(), "9000")) {
                Net.get().getOrderState(PayOrderActivity.this.orderId)
                        .compose(Transformer.<AuthTask>switchSchedulers())
                        .subscribe(new CommonObserver<AuthTask>() {
                            @Override
                            protected void onError(String errorMsg) {

                            }

                            @Override
                            protected void onSuccess(AuthTask authTask) {

                            }
                        });
                Toast.makeText(PayOrderActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(PayOrderActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
        }
    };
    int orderId = -1;
    String orderNo = "";
    @BindView(R.id.out_of_time_tips)
    TextView outOfTimeTips;
    @BindView(R.id.overtime_pay_title)
    TextView overtimePayTitle;
    double payMoney = -1.0D;
    @BindView(R.id.pay_title)
    TextView payTitle;
    @BindView(R.id.pay_type_choose)
    RelativeLayout payTypeChoose;
    int postagePayType = -1;
    private ProgressDialog progressDialog;
    Runnable runnable = new Runnable() {
        public void run() {
            if (PayOrderActivity.this.countdownTime > 1000L) {
                PayOrderActivity.this.isNotOvertime = true;
                long l2 = PayOrderActivity.this.countdownTime / 1000L;
                long l1 = l2 / 3600L;
                long l3 = l2 % 3600L;
                l2 = l3 / 60L;
                l3 %= 60L;
                String str1 = l1 + "";
                if (l1 < 10L) {
                    str1 = "0" + l1;
                }
                String str2 = l2 + "";
                if (l2 < 10L) {
                    str2 = "0" + l2;
                }
                String str3 = l3 + "";
                if (l3 < 10L) {
                    str3 = "0" + l3;
                }
                PayOrderActivity.this.timer_view.setText(str1 + ":" + str2 + ":" + str3);
                PayOrderActivity.this.handler.postDelayed(this, 1000L);
                return;
            }
            PayOrderActivity.this.isNotOvertime = false;
            PayOrderActivity.this.payTitle.setVisibility(View.GONE);
            PayOrderActivity.this.timer_view.setVisibility(View.GONE);
            PayOrderActivity.this.outOfTimeTips.setVisibility(View.GONE);
            PayOrderActivity.this.overtimePayTitle.setVisibility(View.VISIBLE);
            PayOrderActivity.this.payTypeChoose.setVisibility(View.GONE);
            PayOrderActivity.this.handler.removeCallbacks(new Runnable() {
                @Override
                public void run() {

                }
            });
        }
    };
    @BindView(R.id.stv_alipay)
    SuperTextView stv_alipay;
    @BindView(R.id.stv_union_pay)
    SuperTextView stv_union_pay;
    @BindView(R.id.stv_wechat_pay)
    SuperTextView stv_wechat_pay;
    private String timefromServer;
    @BindView(R.id.timer_pay)
    TextView timer_view;
    @BindView(R.id.tips)
    TextView tips;
    @BindView(R.id.tv_money)
    TextView tv_money;
    int userId = -1;

    private void getTimeDuring() {
        this.chaoshitime = 14400000L;
        if (this.postagePayType == 0) {
            this.chaoshitime = 14400000L;
        } else {
            Object localObject = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                localObject = ((SimpleDateFormat) localObject).parse(this.timefromServer);
                long l1 = new Date().getTime();
                long l2 = ((Date) localObject).getTime();
                this.countdownTime = (this.chaoshitime - (l1 - l2));
                this.handler.postDelayed(this.runnable, 1000L);

                if (this.postagePayType != 1) {
                }
                this.chaoshitime = 86400000L;
            } catch (ParseException localParseException) {

            }
        }
    }

    private void pay(int paramInt) {
        Tools.addActivity(this);
        switch (paramInt) {
            case 1:
                Net.get().getOrderInfo(this.orderId)
                        .compose(Transformer.<AuthTask>switchSchedulers())
                        .subscribe(new CommonObserver<AuthTask>() {
                            @Override
                            protected void onError(String errorMsg) {

                            }

                            @Override
                            protected void onSuccess(AuthTask authTask) {

                            }
                        });
                break;
        }
        Log.d("wxOrderInfo", "进来了");
        Net.get().getWXOrderInfo(this.orderId)
                .compose(Transformer.<WXOrderInfo>switchSchedulers())
                .subscribe(new CommonObserver<WXOrderInfo>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(WXOrderInfo wxOrderInfo) {

                    }
                });
    }

    private void setClickEvent() {
        this.stv_wechat_pay.setOnClickListener(this);
        this.stv_alipay.setOnClickListener(this);
        this.stv_union_pay.setOnClickListener(this);
        this.back.setOnClickListener(this);
    }

    private void success() {
        Net.get().cancleOrder(this.orderId, this.userId, 1)
                .compose(Transformer.<CancleOrderModel>switchSchedulers())
                .subscribe(new CommonObserver<CancleOrderModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(CancleOrderModel cancleOrderModel) {

                    }
                });
    }

    public void getOrderMsg() {
        Net.get().getOrderDetail(this.orderId, this.userId)
                .compose(Transformer.<OrderDetail>switchSchedulers())
                .subscribe(new CommonObserver<OrderDetail>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(OrderDetail orderDetail) {

                    }
                });
    }

    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.stv_wechat_pay:
                if (!this.isChoosePayMode) {
                    this.isChoosePayMode = true;
                    Log.d("wxOrderInfo", "我点击了微信");
                    if (this.payMoney <= 0.0D) {
                        Tools.addActivity(this);
                        updateSatetIsOne(this.orderNo, this.payMoney, 2);
                        return;
                    }
                    pay(2);
                    return;
                }
                Toast.makeText(this, "页面跳转中", Toast.LENGTH_SHORT).show();
                break;
            case R.id.stv_alipay:
                if (!this.isChoosePayMode) {
                    this.isChoosePayMode = true;
                    if (this.payMoney <= 0.0D) {
                        Tools.addActivity(this);
                        updateSatetIsOne(this.orderNo, this.payMoney, 1);
                        return;
                    }
                    pay(1);
                    return;
                }
                Toast.makeText(this, "页面跳转中", Toast.LENGTH_SHORT).show();
                break;
            case R.id.stv_union_pay:
                if (!this.isChoosePayMode) {
                    this.isChoosePayMode = true;
                    if (this.payMoney <= 0.0D) {
                        Tools.addActivity(this);
                        updateSatetIsOne(this.orderNo, this.payMoney, 3);
                        return;
                    }
                    pay(3);
                    return;
                }
                Toast.makeText(this, "页面跳转中", Toast.LENGTH_SHORT).show();
                break;
        }
        if (this.isNotOvertime) {
            this.clearHistorySearhPopu = new ClearHistorySearhPopu(this, new OnClickListener() {
                public void onClick(View paramAnonymousView) {
                    if (PayOrderActivity.this.fromDetailPage != 1) {
                        Intent intent = new Intent(PayOrderActivity.this, OrderDetailActivity.class);
                        intent.putExtra("orderId", PayOrderActivity.this.orderId);
                        PayOrderActivity.this.startActivity(intent);
                    }
                    PayOrderActivity.this.clearHistorySearhPopu.dismiss();
                    PayOrderActivity.this.finish();
                }
            }, Integer.valueOf(12));
            this.clearHistorySearhPopu.showAtLocation(findViewById(R.id.choose_pay_type_page), 17, 0, 0);
            return;
        }
        finish();
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_pay_order);
        ButterKnife.bind(this);
        Tools.finishAll();
        this.userId = getSharedPreferences("JLAppData", 0).getInt("userId", -1);
        Intent intent = getIntent();
        this.payMoney = intent.getDoubleExtra("payMoney", -1.0D);
        this.orderId = intent.getIntExtra("orderId", -1);
        this.orderNo = intent.getStringExtra("orderNo");
        this.postagePayType = intent.getIntExtra("postagePayType", -1);
        this.fromDetailPage = intent.getIntExtra("fromDetailPage", -1);
        if ((this.payMoney != -1.0D) && (this.orderId != -1) && (this.orderNo != null) && (this.orderNo != "") && (this.postagePayType != -1)) {
            getOrderMsg();
            this.tv_money.setText(this.payMoney + "");
            if (this.postagePayType == 1) {
                this.tips.setVisibility(View.VISIBLE);
            } else {
                setClickEvent();
                this.tips.setVisibility(View.GONE);
            }
        }
        Toast.makeText(this, "页面传值错误", Toast.LENGTH_SHORT).show();
    }

    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if (paramInt == 4) {
            if (this.isNotOvertime) {
                this.clearHistorySearhPopu = new ClearHistorySearhPopu(this, new OnClickListener() {
                    public void onClick(View paramAnonymousView) {
                        if (PayOrderActivity.this.fromDetailPage != 1) {
                            Intent intent = new Intent(PayOrderActivity.this, OrderDetailActivity.class);
                            intent.putExtra("orderId", PayOrderActivity.this.orderId);
                            PayOrderActivity.this.startActivity(intent);
                        }
                        PayOrderActivity.this.clearHistorySearhPopu.dismiss();
                        PayOrderActivity.this.finish();
                    }
                }, Integer.valueOf(12));
                this.clearHistorySearhPopu.showAtLocation(findViewById(R.id.choose_pay_type_page), 17, 0, 0);
                return true;
            }
            finish();
            return true;
        }
        return super.onKeyDown(paramInt, paramKeyEvent);
    }

    protected void onResume() {
        super.onResume();
        if (WXPayEntryActivity.resultCode.intValue() == -1) {
            this.isChoosePayMode = false;
            Log.d("aaaWXPayEntryActivity", "-1");
            Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
            WXPayEntryActivity.resultCode = Integer.valueOf(1);
        }
        while (WXPayEntryActivity.resultCode.intValue() != -2) {
            return;
        }
        this.isChoosePayMode = false;
        Log.d("aaaWXPayEntryActivity", "-2");
        Toast.makeText(this, "您已经取消了支付", Toast.LENGTH_SHORT).show();
        WXPayEntryActivity.resultCode = Integer.valueOf(1);
    }

    public void sendPayRequest(WXOrderInfo.ResultDataBean paramResultDataBean) {
        Log.d("wxOrderInf", "开始调用");
        PayReq localPayReq = new PayReq();
        Log.d("aaaaaaaaa", "Appid:" + paramResultDataBean.getAppid());
        localPayReq.appId = paramResultDataBean.getAppid();
        localPayReq.partnerId = paramResultDataBean.getPartnerid();
        localPayReq.prepayId = paramResultDataBean.getPrepayid();
        localPayReq.nonceStr = paramResultDataBean.getNoncestr();
        localPayReq.timeStamp = (paramResultDataBean.getTimestamp() + "");
        localPayReq.packageValue = paramResultDataBean.getPackageX();
        localPayReq.sign = paramResultDataBean.getSign();
        this.api = WXAPIFactory.createWXAPI(this, paramResultDataBean.getAppid());
        this.api.registerApp(paramResultDataBean.getAppid());
        WXPayEntryActivity.orderId = Integer.valueOf(this.orderId);
        WXPayEntryActivity.orderNo = this.orderNo;
        this.api.sendReq(localPayReq);
    }

    public void updateSatetIsOne(String paramString, double paramDouble, int paramInt) {
        Net.get().updateSatetIsOne(paramString, paramDouble, paramInt)
        .compose(Transformer.<PostModel>switchSchedulers())
        .subscribe(new CommonObserver<PostModel>() {
            @Override
            protected void onError(String errorMsg) {

            }

            @Override
            protected void onSuccess(PostModel postModel) {

            }
        });
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/activity/PayOrderActivity.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */